/*
weapon_shot_m3
darkrp_console
*/
/*
"money_printer",
"money_printer_home",
"money_printer_pro",
"drug_lab",
"drug",
"microwave",
"spawned_food",
"gunlab",
"spawned_shipment",
"spawned_weapon",
"spawned_money",
"sent_keypad",
"gmod_button",
"disguise",
"durgz_alcohol",
"durgz_aspirin",
"durgz_cigarette",
"durgz_cocaine",
"durgz_heroine",
"durgz_lsd",
"durgz_mushroom",
"durgz_pcp",
"durgz_water",
"durgz_weed",
"madcow_ammocrate"*/
local itemTypes = { 

}
local playerStuff={

}

Msg("Shadowndacorner RP Wallhack Initialized...\n")
Msg("Checking if entity file exists...\n")
if file.ExistsEx("shackcustoments.txt") then
	Msg("File exists!\n")
else
	Msg("File does not exist!  Creating it...\n")
	file.Write("shackcustoments.txt")
	if file.ExistsEx("shackcustoments.txt") then
		Msg("Success!\n")
	else
		Msg("File creation failed!  You will receive an LUA error in the next section.\n")
	end
end
Msg("Inserting entities...\n")
Msg("==============================\n")
for f, v in pairs(string.Explode(";\n", file.Read("shackcustoments.txt"))) do
	if v!="" then
		table.insert(itemTypes, v)
		Msg("Entity "..f..", "..v.." has been added.\n")
	end
end
Msg("==============================\n")
Msg("Done!\n")

Msg("Building convars...\n")
Msg("==============================\n")
for f, v in pairs(itemTypes) do
	if !ConVarExists("shack_search"..v) then
		CreateClientConVar( "shack_search"..v, 1, true, false )
		if ConVarExists("shack_search"..v) then
			Msg("Convar created for entity "..v.."\n")
		else
			Msg("Convar creation failed for entity "..v.."\n")
		end
	else
		Msg("Convar already exists for entity "..v.."!\n")
	end
end
Msg("==============================\n")
Msg("Done!\n")
Msg("\n")
Msg("Entity list:\n")
Msg("==============================\n")
for f, v in pairs(itemTypes) do
	Msg("Entity "..f.." is "..v.."\n")
end
Msg("==============================\n")
Msg("\n")
Msg("Shadowndacorner RP Wallhack started without any errors!\nHave fun\n")

concommand.Add("shack_printents", function()
	local count=0
	Msg("Shack: Printing entities to console...\n")
	Msg("==============================\n")
	for f, v in pairs(itemTypes) do
		Msg("Entity "..f..": "..v.."\n")
		count=count+1
	end
	Msg("==============================\n")
	Msg("Done printing entities!  "..count.." were returned!\n")
end )

local width=200
local heigth=300
function ShackCtrlPanel()
	CtrlPanel=vgui.Create( "DFrame" )
	CtrlPanel:SetPos( 50,50 )
	if table.Count(itemTypes)>=14 then
		local extra=table.Count(itemTypes)-( 14 )
		heigth=300+( 20*extra+20 )
	else
		heigth=300
	end
	if table.Count(itemTypes) > 25 then
		width=600
	else
		width=300
	end
	CtrlPanel:SetSize( width, heigth )
	CtrlPanel:SetTitle( "SNDC Wallhack Menu" )
	CtrlPanel:SetVisible( true )
	CtrlPanel:SetDraggable( true )
	CtrlPanel:ShowCloseButton( true )
	CtrlPanel:MakePopup()
	// Buttons
	local DermaButton = vgui.Create( "DButton" )
	DermaButton:SetParent( CtrlPanel ) -- Set parent to our "CtrlPanel"
	DermaButton:SetText( "Add Entity" )
	DermaButton:SetPos( 10,30 )
	DermaButton:SetSize( 60, 20 )
	DermaButton.DoClick = function ()
		RunConsoleCommand( "shack_centmenu" ) -- What happens when you press the button
	end
	/*
	local DermaButton = vgui.Create( "DButton" )
	DermaButton:SetParent( CtrlPanel ) -- Set parent to our "CtrlPanel"
	DermaButton:SetText( "Players" )
	DermaButton:SetPos( 90,30 )
	DermaButton:SetSize( 60, 20 )
	DermaButton.DoClick = function ()
		RunConsoleCommand( "shack_players" ) -- What happens when you press the button
	end
	*/
	// Built In Checkboxes
	// Enable/disable
	/*
	local DermaListView = vgui.Create("DListView")
	DermaListView:SetParent(CtrlPanel)
	DermaListView:SetPos(25, 50)
	DermaListView:SetSize(450, 625)
	DermaListView:SetMultiSelect(false)
	DermaListView:AddColumn("Name") -- Add column
	DermaListView:AddColumn("Amount of kills")
	local IsActive
	if GetConVarNumber("shack_showplayers")==1 then
		IsActive="Activated"
	else
		IsActive="Deactivated"
	end
	DermaListView:AddLine( "Players", IsActive )
	
	if GetConVarNumber("shack_showplayerhealth")==1 then
		IsActive="Activated"
	else
		IsActive="Deactivated"
	end
	DermaListView:AddLine( "Show Player Health", IsActive )
	if GetConVarNumber("shack_searchcopsonly")==1 then
		IsActive="Activated"
	else
		IsActive="Deactivated"
	end
	DermaListView:AddLine( "Only Show Cops", IsActive )
	
	for k,v in pairs(itemTypes) do
		if GetConVarNumber("shack_show"..v) == 1 then
			IsActive="Activated"
		else
			IsActive="Deactivated"
		end
		DermaListView:AddLine(v,IsActive) -- Add lines
	end
	DermaListView.OnClickLine = function ( parent, line, isselected )
		local MenuButtonOptions = DermaMenu() -- Creates the menu
		MenuButtonOptions:AddOption("Show", function(  )
			table.insert( playerStuff,  , true )
		end ) -- Add options to the menu
		MenuButtonOptions:AddOption("Hide", function(  )
			table.insert( playerStuff, , false )
		end )
		MenuButtonOptions:Open() -- Open the menu AFTER adding your options
		end */
	
	local CheckBoxThing = vgui.Create( "DCheckBoxLabel", CtrlPanel )
	CheckBoxThing:SetPos( 10,60 )
	CheckBoxThing:SetText( "Enable Wallhack" )
	CheckBoxThing:SetConVar( "rp_wallhackenabled" ) -- ConCommand must be a 1 or 0 value
	CheckBoxThing:SetValue( GetConVarNumber("rp_wallhackenabled") )
	CheckBoxThing:SizeToContents() -- Make its size to the contents. Duh?
	// Search for players
	local CheckBoxThing = vgui.Create( "DCheckBoxLabel", CtrlPanel )
	CheckBoxThing:SetPos( 10,80 )
	CheckBoxThing:SetText( "Search for players" )
	CheckBoxThing:SetConVar( "shack_searchplayers" ) -- ConCommand must be a 1 or 0 value
	CheckBoxThing:SetValue( GetConVarNumber("shack_searchplayers") )
	CheckBoxThing:SizeToContents() -- Make its size to the contents. Duh?
	// Show players' health
	local CheckBoxThing = vgui.Create( "DCheckBoxLabel", CtrlPanel )
	CheckBoxThing:SetPos( 10,100 )
	CheckBoxThing:SetText( "Show player health" )
	CheckBoxThing:SetConVar( "shack_showplayerhealth" ) -- ConCommand must be a 1 or 0 value
	CheckBoxThing:SetValue( GetConVarNumber("shack_showplayerhealth") )
	CheckBoxThing:SizeToContents() -- Make its size to the contents. Duh?
	
	// Only search for cops
	local CheckBoxThing = vgui.Create( "DCheckBoxLabel", CtrlPanel )
	CheckBoxThing:SetPos( 10,120 )
	CheckBoxThing:SetText( "Search only for cops" )
	CheckBoxThing:SetConVar( "shack_searchcopsonly" ) -- ConCommand must be a 1 or 0 value
	CheckBoxThing:SetValue( GetConVarNumber( "shack_searchcopsonly" ) )
	CheckBoxThing:SizeToContents() -- Make its size to the contents. Duh?
	// The rest of the item types
	for f, v in pairs(itemTypes) do
		local CheckBoxThing = vgui.Create( "DCheckBoxLabel", CtrlPanel )
		if tonumber(f)<=25 then
			CheckBoxThing:SetPos( 10,140+tonumber(f)*20 )
		else
			CheckBoxThing:SetPos( 40,140+tonumber(f)*20 )
		end
		CheckBoxThing:SetText( "Search for "..v )
		CheckBoxThing:SetConVar( "shack_search"..v ) -- ConCommand must be a 1 or 0 value
		CheckBoxThing:SetValue( GetConVarNumber("shack_search"..v) )
		CheckBoxThing:SizeToContents() -- Make its size to the contents. Duh?
		Msg("Entity "..f..": "..v.."\n")
	end
	
end
concommand.Add("shack_panel", ShackCtrlPanel)
/*
for f, v in pairs(player.GetAll()) do
	table.insert( playerStuff, v:UniqueID(), false )
end
*/
/*
function shackPlayerList()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 50,50 )
	DermaPanel:SetSize( 500, 700 )
	DermaPanel:SetTitle( "List of Players" )
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()
	
	for k,v in pairs(player.GetAll()) do
		local DermaButton = vgui.Create( "DButton" )
		DermaButton:SetParent( DermaPanel ) -- Set parent to our "CtrlPanel"
		DermaButton:SetText( v:Nick() )
		DermaButton:SetPos( 10,k*30 )
		DermaButton:SetSize( string.len(v:Nick())*20, 20 )
		DermaButton.DoClick = function ( btn )
		local MenuButtonOptions = DermaMenu() -- Creates the menu
		MenuButtonOptions:AddOption("Show", function()
			table.insert( playerStuff, v:UniqueID(), true )
		end ) -- Add options to the menu
		MenuButtonOptions:AddOption("Hide", function()
			table.insert( playerStuff, v:UniqueID(), false )
		end )
		MenuButtonOptions:Open() -- Open the menu AFTER adding your options
		end
	end
end
concommand.Add("shack_players", shackPlayerList)
*/

function customEntMenu()
	local CentmPanel = vgui.Create( "DFrame" )
	CentmPanel:SetSize( 195, 60 )
	CentmPanel:SetPos( 50,50 )
	CentmPanel:SetTitle( "SNDC Wallhack Custom Entity" )
	CentmPanel:SetVisible( true )
	CentmPanel:SetDraggable( true )
	CentmPanel:ShowCloseButton( true )
	CentmPanel:MakePopup()
	
	local DermaText = vgui.Create( "DTextEntry", CentmPanel )
	DermaText:SetPos( 10,30 )
	DermaText:SetTall( 20 )
	DermaText:SetWide( 180 )
	DermaText:SetEnterAllowed( true )
	DermaText.OnEnter = function()
		local v=DermaText:GetValue()
		local newPos=table.Count(itemTypes)+1
		LocalPlayer():PrintMessage( HUD_PRINTTALK, "You have added the entity "..DermaText:GetValue()) -- What happens when you press enter
		CreateClientConVar( "shack_search"..v, 1, true, false )
		filex.Append("shackcustoments.txt", v..";\n")
		table.insert( itemTypes, v )
		local extra=table.Count(itemTypes)-( 14 )
		Msg("Checkbox added for "..v.."\n")
		CtrlPanel:SetSize( width, heigth+( 20*extra+20 ) )
		Msg("CtrlPanel resized \n")
		CentmPanel:SetVisible( false )
end
end
concommand.Add("shack_centmenu", customEntMenu)

/*
// Autoshoot
local TriggerBot = CreateClientConVar( "triggerbot_enabled", 0, true, false )
hook.Add( "Think", "Triggerbot", function()
	local Target = LocalPlayer():GetEyeTrace().Entity
	if TriggerBot:GetInt() == 1 and LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() and ( Target:IsPlayer() or Target:IsNPC() ) then
		if !Firing then
			RunConsoleCommand( "+attack" )
			LocalPlayer():GetActiveWeapon().SetNextPrimaryFire( LocalPlayer():GetActiveWeapon() )
			Firing = true
		else
			RunConsoleCommand( "-attack" ) 
			Firing = false
		end
	end
end )
*/

// Aimbot
/*
local TriggerAim=CreateClientConVar("triggeraim_enabled", 0, true, false)
function aimbot()
	if TriggerAim:GetInt() == 1 and LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():IsValid() then
		local ply = LocalPlayer()
		local trace = util.GetPlayerTrace( ply )
		local traceRes = util.TraceLine( trace )
		if traceRes.HitNonWorld then
			local target = traceRes.Entity
			if target:IsPlayer() then
				local targethead = target:LookupBone("ValveBiped.Bip01_Head1")
				local targetheadpos,targetheadang = target:GetBonePosition(targethead)-10
				ply:SetEyeAngles((targetheadpos - ply:GetShootPos()):Angle())
			end
		end
	end
end
hook.Add("Think","aimbot",aimbot)
*/
// Wallhack
local WallEnabled=CreateClientConVar( "rp_wallhackenabled", 1, true, false )
local showPlayers=CreateClientConVar( "shack_searchplayers", 1, true, false )
local OnlyShowCops=CreateClientConVar( "shack_searchcopsonly", 0, true, false )
local showHealth=CreateClientConVar( "shack_showplayerhealth", 1, true, false )
function wallhack()
	if WallEnabled:GetInt()==1 then
	if showPlayers:GetInt()==1 then
	for f,v in pairs(player.GetAll()) do
		local l2=""
		if GetConVarNumber("shack_searchcopsonly")==0 then
			local Position = ( v:GetPos() + Vector( 0,0,90 ) ):ToScreen()
			local Name = v:Name()
			local teamStuff="Team: "..team.GetName(v:Team())
			if GetConVarString("sv_gamemode")=="terrortown" then
				if v:IsTraitor() then
					teamStuff="Traitor"
				else
				if v:IsDetective() then
					teamStuff="Detective"
				else
					teamStuff="Innocent"
				end
				end
			end
			if showHealth:GetInt()==1 then
				l2="Health: "..v:Health().." "..teamStuff
			else
				l2=teamStuff
			end
			local length = 0
			if string.len(Name) > string.len(l2) then
				length=string.len(Name)+2
				else
				length=string.len(l2)+2
			end
			if v!=LocalPlayer() then
				draw.RoundedBox( 3, Position.x-5*length, Position.y, length*10, 38, Color( 50, 50, 50, 150 ) )
				draw.DrawText( Name, "MenuLarge", Position.x, Position.y, team.GetColor(v:Team()), 1 )
				//Msg(Name..": "..string.len(Name).."\n")
				//Msg(string.len(Name).."\n")
				draw.DrawText( l2,"MenuLarge", Position.x, Position.y+15, team.GetColor(v:Team()), 1 )
	//			draw.DrawText( v:Health(),"MenuLarge", Position.x, Position.y+30, team.GetColor(v:Team()), 1 )
			end
	else
		if GetConVarNumber("shack_searchcopsonly")==0 then
			local Position = ( v:GetPos() + Vector( 0,0,90 ) ):ToScreen()
			local Name = v:Name()
			local l2="Health: "..v:Health().." Team: "..team.GetName(v:Team())
			local length = 0
			if string.len(Name) > string.len(l2) then
				length=string.len(Name)+2
				else
				length=string.len(l2)+2
			end
			if v!=LocalPlayer() then
				draw.RoundedBox( 3, Position.x-5*length, Position.y, length*10, 38, Color( 50, 50, 50, 150 ) )
				draw.DrawText( Name, "MenuLarge", Position.x, Position.y, team.GetColor(v:Team()), 1 )
				//Msg(Name..": "..string.len(Name).."\n")
				//Msg(string.len(Name).."\n")
				draw.DrawText( l2,"MenuLarge", Position.x, Position.y+15, team.GetColor(v:Team()), 1 )
	//			draw.DrawText( v:Health(),"MenuLarge", Position.x, Position.y+30, team.GetColor(v:Team()), 1 )
		end
	else
		local Position = ( v:GetPos() + Vector( 0,0,90 ) ):ToScreen()
		local Name = v:Name()
		local length = 0
		if string.len(Name) > string.len(l2) then
			length=string.len(Name)+2
			else
			length=string.len(team.GetName(v:Team()))+2
		end
		if v!=LocalPlayer() and v:Team()==TEAM_POLICE or v:Team()==TEAM_CHIEF or v:Team()==TEAM_MAYOR then
			draw.RoundedBox( 3, Position.x-5*length, Position.y, length*10, 38, Color( 50, 50, 50, 150 ) )
			draw.DrawText( Name, "MenuLarge", Position.x, Position.y, team.GetColor(v:Team()), 1 )
			//Msg(Name..": "..string.len(Name).."\n")
			//Msg(string.len(Name).."\n")
			draw.DrawText( "Health: "..v:Health().." Team: "..team.GetName(v:Team()),"MenuLarge", Position.x, Position.y+15, team.GetColor(v:Team()), 1 )
//			draw.DrawText( v:Health(),"MenuLarge", Position.x, Position.y+30, team.GetColor(v:Team()), 1 )
		end
	end
	end
	end
	end
	for k, t in pairs(itemTypes) do
		if GetConVarNumber("shack_search"..t)==1 then
		for f, v in pairs(ents.FindByClass(t)) do
			local Position = ( v:GetPos() + Vector( 0,0,0 ) ):ToScreen()
			local col = ( ( ( tonumber(k)/( table.Count(itemTypes) )*( 100/table.Count(itemTypes) ) ) ) )
			local CLname
			if v.PrintName != nil then
				CLname = v.PrintName
			else	
				CLname="Unrecognized Entity"
			end
			if v:GetClass()!="gmod_button" or v:GetClass()!="spawned_shipment" then
				draw.RoundedBox( 3, Position.x-5*string.len(v.PrintName), Position.y, string.len(v.PrintName)*10, 23, Color( 100, 100, 100, 150 ) )
			end
			if v:GetClass()=="spawned_shipment" then
			
				local content = v.dt.contents or ""
				local contents = CustomShipments[content]
				if not contents then return end
				contents = contents.name
				Clname="Shipment of "..contents
				draw.RoundedBox( 3, Position.x-5*string.len(Clname), Position.y, string.len(Clname)*10, 23, Color( 100, 100, 100, 150 ) )
				draw.DrawText( "Shipment of "..contents, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
			else
				if v:GetClass()=="spawned_weapon" then
					local class = tostring( v.Entity.weaponclass )
					draw.DrawText( "Weapon", "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
				else
					if v:GetClass()=="spawned_money" then
						local amount=v.dt.amount
						draw.DrawText( "$"..amount, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
					else
					if v:GetClass()=="gmod_button" then
						Clname="Button"
						draw.RoundedBox( 3, Position.x-5*string.len(Clname), Position.y, string.len(Clname)*10, 23, Color( 100, 100, 100, 150 ) )
						draw.DrawText( "Button", "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
					else
					if v:GetClass()=="asent_keypad" then
						draw.DrawText( CLName, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
					else
					if v:GetClass()=="prop_door_rotating" then
						CLname="Door"
						draw.DrawText( CLname, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
					else
					if v:GetClass()=="prop_physics" then
						CLname=v:GetModel()
						draw.DrawText( CLname, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
					else
						draw.DrawText( CLname, "MenuLarge", Position.x, Position.y, Color( 255, 2*col, 5*col, 255 ), 1 )
						end
						
					end
					end
					end
					end
				end
				end
			end
		end
	
	end
	end
end
hook.Add( "RenderScreenspaceEffects", "Wallhack", wallhack)

 
function GModeWarnings()
	// Declaring variables
	local ply=LocalPlayer()
	local GameMode=GetConVarString( "sv_gamemode" )
	local hostname=GetConVarString( "hostname" )
	// Gamemode-specific Warnings/ Messages
	if GameMode=="terrortown" then
		chat.AddText( "SHACK: This server is running Trouble in Terrorist Town, and you will only be able to see terrorists if you ARE a terrorist.  There is no way around this." )
	end
end
hook.Add( "Initialize", "shack_gmwarnings", GModeWarnings )